mod_rewrite, a beginner’s guide (with examples)

Until recently, I only had the vaguest of ideas of what mod_rewrite was, and I certainly had no clue about how to use it. So, when I started designing this site, I decided to delve into the wonders that are the mod_rewrite Apache module.

So, what is mod_rewrite for?

Simply, mod_rewrite is used for rewriting a URL at the server level, giving the user output for that final page. So, for example, a user may ask for http://www.somesite.com/widgets/blue/, but will really be given http://www.somesite.com/widgets.php?colour=blue by the server. Of course, the user will be none the wiser to this little bit of chicanery.

On workingwith.me.uk, I use mod_rewrite to redirect all pages to one central PHP page, which then loads the data that the user wanted from an external data file. Lots of people use mod_rewrite to show an “alternative” image when people are hotlinking directly to their images.

What do I need to get mod_rewrite working?

There’s pretty much only one thing you’ll need to get mod_rewrite working for you, and that’s to have the mod_rewrite module installed on your Apache server!

For the purpose of this article, I’m going to assume that you don’t have access to view or edit the Apache server httpd.conf file, so the easiest way to check whether the mod_rewrite module is installed will be to look on your phpinfo page. If you’ve not already created one of these for yourself, just copy and paste the following code into an new text file using your favourite text editor, save it as phpinfo.php, and upload it to your server:

<?php phpinfo(); ?>

Load that page up in your web browser, and perform a search for “mod_rewrite”. All being well, you’ll find it in the “Apache loaded modules” section of the page. If it isn’t there, you’ll have to contact your hosting company and politely ask them to add it to the Apache configuration.

Assuming the mod_rewrite module is loaded, then you’re good to go!

A simple mod_rewrite example

So, let’s write a simple mod_rewrite example. This isn’t going to be anything fancy; we’re just going to redirect people who ask for alice.html to the page bob.html instead. First, let’s create the Alice and Bob pages. Below is Alice’s webpage - create a similar one for Bob.

<html>
   <head>
      <title>Alice's webpage</title>
   </head>
   <body>
      <p>
         This is Alice's webpage
      </p>
   </body>
</html>

Upload both of these to your web server, and check that you can view both of them. Now comes the fun - we’re going to add a couple of lines to your .htaccess file. The .htaccess file is a text file which contains Apache directives. Any directives which you place in it will apply to the directory which the .htaccess file sits in, and any below it. To ours, we’re going to add the following:

RewriteEngine on
RewriteRule ^alice.html$ bob.html

Upload this .htaccess file to the same directory as alice.html and bob.html, and reload Alice’s page. You should see Bob’s page being displayed, but Alice’s URL. If you still see Alice’s page being displayed, then check you’ve followed the instructions correctly (you may have to clear your cache). If things still aren’t working for you, then contact your technical support people and ask them to enable mod_rewrite and the FileInfo override in their httpd.conf file for you

The structure of a RewriteRule

RewriteRule Pattern Substitution [OptionalFlags]

The general structure of a RewriteRule is fairly simple if you already understand regular expressions. This article isn’t intended to be a tutorial about regular expressions though - there are already plenty of those available. RewriteRules are broken up as follows:

RewriteRule

This is just the name of the command.

Pattern

A regular expression which will be applied to the “current” URL. If any RewriteRules have already been performed on the requested URL, then that changed URL will be the current URL.

Substitution

Substitution occurs in the same way as it does in Perl, PHP, etc.

You can include backreferences and server variable names (%{VARNAME}) in the substitution. Backreferences to this RewriteRule should be written as $N, whereas backreferences to the previous RewriteCond should be written as %N.

A special substitution is -. This substitution tells Apache to not perform any substitution. I personally find that this is useful when using the F or G flags (see below), but there are other uses as well.

OptionalFlags

This is the only part of the RewriteRule which isn’t mandatory. Any flags which you use should be surrounded in square brackets, and comma separated. The flags which I find to be most useful are:

  • F - Forbidden. The user will receive a 403 error.

  • L - Last Rule. No more rules will be proccessed if this one was successful.

  • R[=code] - Redirect. The user’s web browser will be visibly redirected to the substituted URL. If you use this flag, you must prefix the substitution with http://www.somesite.com/, thus making it into a true URL. If no code is given, then a HTTP reponse of 302 (temporarily moved) is sent.

A full list of flags is given in the Apache mod_rewrite manual.

A slightly more complicated mod_rewrite example

Let’s try a slightly more meaty example now. Suppose you have a web page which takes a parameter. This parameter tells the page how to be displayed, and what content to pull into it. Humans don’t tend to like remembering the additional syntax of query strings for URLs, and neither do search engines. Both sets of people seem to much prefer a straight URL, with no extra bits tacked onto the end.

In our example, you’ve created a main index page with takes a page parameter. So, a link like index.php?page=software would take you to a software page, while a link to index.php?page=interests would take you to an interests page. What we’ll do with mod_rewrite is to silently redirect users from page/software/ to index.php?page=software etc.

The following is what needs to go into your .htaccess file to accomplish that:

RewriteEngine on
RewriteRule ^page/([^/\.]+)/?$ index.php?page=$1 [L]

Let’s walk through that RewriteRule, and work out exactly what’s going on:

^page/

Sees whether the requested page starts with page/. If it doesn’t, this rule will be ignored.

([^/.]+)

Here, the enclosing brackets signify that anything that is matched will be remembered by the RewriteRule. Inside the brackets, it says “I’d like one or more characters that aren’t a forward slash or a period, please”. Whatever is found here will be captured and remembered.

/?$

Makes sure that the only thing that is found after what was just matched is a possible forward slash, and nothing else. If anything else is found, then this RewriteRule will be ignored.

index.php?page=$1

The actual page which will be loaded by Apache. $1 is magically replaced with the text which was captured previously.

[L]

Tells Apache to not process any more RewriteRules if this one was successful.

Let’s write a quick page to test that this is working. The following test script will simply echo the name of the page you asked for to the screen, so that you can check that the RewriteRule is working.

<html>
   <head>
      <title>Second mod_rewrite example</title>
   </head>
   <body>
      <p>
         The requested page was:
         <?php echo $_GET['page']; ?>
      </p>
   </body>
</html>

Again, upload both the index.php page, and the .htaccess file to the same directory. Then, test it! If you put the page in http://www.somesite.com/mime_test/, then try requesting http://www.somesite.com/mime_test/page/software. The URL in your browser window will show the name of the page which you requested, but the content of the page will be created by the index.php script! This technique can obviously be extended to pass multiple query strings to a page - all you’re limited by is your imagination.

Conditional Statements and mod_rewrite

But what happens when you start getting people hotlinking to your images (or other files)? Hot linking is the act of including an image, media file, etc from someone else’s server in one of your own pages as if it were your own. Obviously, as a webmaster, there are plenty of times when you don’t want people doing that. You’ll almost certainly have seen examples where someone has linked to one image on a website, only for a completely different, “nasty” one to be shown instead. So, how is this done?

It’s pretty simple really. All it takes are a couple of RewriteCond statements in your .htaccess file.

RewriteCond statements are as they sound - conditional statements for RewriteRules. The basic format for a RewriteCond is RewriteCond test_string cond_pattern. For our purpose, we will set the test_string to be the HTTP_REFERER. If the test string is neither empty nor our own server, then we will serve an alternative (low bandwidth) image, which tells the person who is hotlinking off for stealing our bandwidth.

Here’s how we do that:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?somesite.com/.*$ [NC]
RewriteRule \.(gif|jpg|png)$ http://www.somesite.com/nasty.gif [R,L]

Here, the RewriteRule will only be performed if all the preceeding RewriteConds are fulfilled. In the second RewriteCond, [NC] simply means “No Case”, so it doesn’t matter whether the domain name was written in upper case, lower case or a mixture of the two. So, any requests for gif, jpg or png files from referers other than somesite.com will result in your “nasty” image being shown instead.

The [R,L] in the RewriteRule simply means “Redirect, Last”. So, the RewriteRule will visibly redirect output to “nasty.gif” and no more RewriteRules will be performed on this URL.

If you simply don’t want the hot linkers to see any image at all when they hot link to your images, then simply change the final line to RewriteRule \.(gif|jpg|png)$ - [F]. The - means “don’t rewrite the requested URL”, and the [F] means “Forbidden”. So, the hot linker will get a “403 Forbidden message”, and you don’t end up wasting your bandwidth.

Conclusion

mod_rewrite is an incredibly handy tool to have in your arsenal. This article only scratched the surface of what is possible with mod_rewrite, but should have given you enough information to go out and start mod_rewriting history yourself!

References

Apache module mod_rewrite - Apache’s big long document about the mod_rewrite module.

The Definitive Guide to Apache mod_rewrite - If you’re serious about learning how to use mod_rewrite and need more detail than you got in this article, then I can sincerely recommend buying The Definitive Guide to Apache mod_rewrite.

If you enjoyed reading this and would like other people to read it as well, please add it to del.icio.us, digg or furl.

If you really enjoyed what you just read, why not buy yourself something from Amazon? You get something nice for yourself, and I get a little bit of commission to pay for servers and the like. Everyone's a winner!

comments (41) | write a comment | permalink | View blog reactions

Comments

  1. by Anonymous on October 22, 2005 08:41 AM

    Fancy article, very helpful. Thanks for share it.

    Could you write some samples for the solution to impleentation of such as secondary domain things.

  2. by Anonymous on October 23, 2005 07:55 PM

    This is an excellent article. The rewriting of http://www.site.com/content to http://www.site.com/?id=content is the most interesting one to me, although I’m curious how one would rewrite http://www.site.com/content/edit to something like http://www.site.com/?id=content&action=edit. Now THAT would be excellent!

  3. by Neil Crosby [TypeKey Profile Page] on October 23, 2005 08:18 PM

    Thanks for the complement. In fact, I did write a blog entry about how to do just that a couple of weeks ago. You can find the relevent entry at: mod_rewriting an entire site

  4. by Anonymous on October 30, 2005 02:58 AM

    A most excellent article, many thanks. Unfortunately, I fell over it just as I had solved my problem :( Am appending the problem and the solution in case it’s useful:

    Problem: how to determine which language to use when serving files, and then make it look to the user as if the files are being served out of a language-specific directory (trying to be helpful) Content negotiation wasn’t the answer, as the content is dbase driven. So I wanted to append a query string to all urls, but have it be invisible to users.

    Solution: if ( mysite.com/ is requested ) then serve up mysite.com/index.html and determine user’s preferred language from the browser. if lang. preference not set, default to english and set $lang=’en’

    then parse all docs thusly:

    # rewrite all requests for language-specific files
    RewriteEngine on
    Options +FollowSymlinks
    RewriteRule ^en/(.*)$ $1?lang=en [NC,L]
    RewriteRule ^de/(.*)$ $1?lang=de [NC,L]
    RewriteRule ^fr/(.*)$ $1?lang=fr [NC,L]

    so eg. all requests for german files look like this: http://mysite.com/de/valley/page.html and bookmarked links automatically come up with the correct language. Look Ma, no cookies!

    By the way, if you know of a better solution, please don’t hesitate to tell me.

  5. by grimboy.videntity.org on December 2, 2005 11:35 PM

    Yes, also you might want to use the link tag to specify other language versions as a schematic thing. Like it says near the bottom of http://www.w3.org/TR/REC-html40/struct/links.html

  6. by Anonymous on December 31, 2005 11:33 PM

    this is the FIRST mod_rewrite tutorial that I have found that actually explains it simply. Now at http://barnt.org:81/~jared/ all my links are redirected as http://barnt.org:81/~jared/id/theidoftheblogpost . It took only a few minutes thanks to this tutorial.

  7. by Anonymous on January 1, 2006 04:19 PM

    There is a massive bug in modrewrite in current Apache releases: modrewrite strips one level of URL encoding between matched strings and output values. If your incoming URL happens to contain URL encoded values, it will thus generate invalid URLs. for example:

    RewriteRule ^something/(.*) something.php?var=$1

    If you pass in a URL like

    something/hello%20world

    It will be rewritten as:

    something.php?var=hello world

    which is not a valid URL.

    It happens that this is bearable for many cases (also why it has not been fixed), as URL encoding does not affect many string values, but if you’re doing anything like passing entire URLs as parameters (e.g. for a redirector), this can cause major problems. This problem has nothing to do with the [NE] option, which is concerned with allowing URL encoded values in the rewrite rule itself, not the string it is applied to.

  8. by tomw on January 2, 2006 02:57 AM

    I have had a go at solving the problem below.

    /pictures/index.php?path=&img=test.jpg
    /pictures/index.php?path=prague&img=DSC01062.JPG

    The idea being that the url becomes /pictures/test

    The only thing with this is that I am not that great at finding the solution. Also, Blorp in creating url direct from the directory and file structure so /pictures/prague/DSC01062.JPG really is a url also. I could use /pictures/prague/DSC01062 without the .JPG file extension but have found that this is a bit hard.

    Example:

    <IfModule mod_rewrite.c>
    #RewriteEngine On
    RewriteBase /pictures/
    RewriteEngine on
    RewriteRule ^alice.html$ bob.html
    #RewriteRule ^page/([^/\.]+)/?$ index.php?page=$1 [L]
    #/pictures/index.php?path=&img=test.jpg
    #/pictures/index.php?path=prague&img=DSC01062.JPG
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^\.\?/]+)/([0-9]+)$ /pictures/index.php?path=$1&img=$2 [QSA]
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^\.\?/]+)/([A-Za-z_0-9\-]+)$ /pictures/index.php?path=$1&img=$2 [QSA]
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^\.\?/]+)/$ /pictures/$1 [R]
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^\.\?/]+)$ /pictures/index.php?path=$1 [QSA]
    </IfModule>

    Cheers

  9. by Rajan Vohra on February 16, 2006 11:03 AM

    I want to redirect the user if he/she directly enters the address of the page in the address bar, but wants to permit the users who enter the page after passing through the authentication page. please suggest the solutions

    Thanks

  10. by Paul Ingram on February 27, 2006 08:34 AM

    Rajan,

    I would have thought this would be something you can do in the language you’re using. In PHP, I assign the login status to a variable which is included with every page. If the user hits a page requiring login, it’s something like:

    if ($login==0) {
      # Not logged in, redirect
      header('location: otherpage.php');
      die;
    }
    

    Note that because I’ve used the header command and therefore am modifying the HTTP headers, this must go before any output to the browser.

    Hope this helps…

    Paul

  11. by Matt on February 27, 2006 06:08 PM

    Great tutorial. I’m just learning about Mod_Rewrites. Unfortunately, I’m still having problems including a query string in my redirect. I’m trying to do the following but need some help:

    http://www.example.com/index.php?pageId=0&start=0

    needs to redirect to:

    http://www.example.com/index.php?pageId=117&start=0

    This is what I tried in my .htaccess dir, but it’s not doing anything:

    RewriteEngine on
    RewriteRule ^gallery/index.php?pageId=0&start=0 gallery/index.php?pageId=117&start=0 [L]
    
  12. by Neil Crosby [TypeKey Profile Page] on March 12, 2006 03:46 PM

    Matt: Sorry it’s taken me a while to reply to you, and I hope you’ve managed to find the solution to your problem elsewhere by now!

    Just in case you haven’t though, here we go. I’ve not actually checked that this works, but even if it doesn’t it should give you somewhere to go from:

    RewriteCond %{QUERY_STRING} ^pageId=0&start=0$
    RewriteRule ^gallery/index.php gallery/index.php?pageId=117&start=0
    

    Basically, the query string is not part of the URL that the RewriteRule looks at, so you have to check this in a RewriteCond beforehand. Basically, the rule above says “Is the query string for this URL ‘pageId=0&start=0’? It is? Excellent! Now, does the URL I’ve been given start with ‘gallery/index.php’? It does? Fantastic! Lets rewrite the user to ‘gallery/index.php?pageId=117&start=0’.”

    One thing to note is that if you were wanting to redirect the user to a different URL with the same query string, then you could use the QSA (query string append) flag on the end of the rule to tack the original query string onto the redirected URL.

  13. by Sumer on March 22, 2006 09:50 AM

    Where exactly is

    http://www.site.com/content to http://www.site.com/?id=content

    with mod rewrite?

    I dont see it on this web site.

  14. by tarek on March 23, 2006 01:12 PM

    please help me to convert dynamic url to static url

  15. by James on March 25, 2006 10:24 PM

    On my site: www.moving-butler.com, I have been trying to test a way to convert a dynamic site I manage. The dynamic site outputs dynamic links. Could I use mod_rewrite to change everything in the web site so it would appear static for the search engines, or would I have to rewrite the entire PHP script set?

  16. by Bobby on March 29, 2006 07:53 AM

    Thank you so much for this article! You made mod_rewrite understandable. I finally can implement this on my website!!

  17. by Dawson on April 3, 2006 04:15 AM

    I think I love you. sigh Four hours of fussing over a line of code, and then I find this and fix it in a matter of minutes…

  18. by Great Stuff Lik-0r on May 20, 2006 05:11 PM

    Hey my friend,

    thanks a lot. I had grasp on regular expressions before, which is pretty simple. But I learned to properly use for mod_rewrite here in your tutorial. Great stuff man, and I like it cause I am the Great Stuff Lik-0r :)

    Cheers man, keep up the good work!!

  19. by Maulana Kurniawan on May 26, 2006 06:53 AM

    Hi,

    I’m quite sure that this article is very usefull, but my local-private-server modrewrite won’t work…I’m using Fedora Core 5, Apache2, and modrewrite loaded. All of mod_rewrite function didn’t shown up. I’ve search through google, fedora forum, apache documentation, etc, nothing usefull for me. Could you or someine please help me?

    Thanks a lot.

  20. by Apanath on May 27, 2006 10:04 AM

    I have my URL as

    http://www.mydomain.com/menu.php?result=education%20online

    I have written the rule as

    ^(.)/ menu.php?result=$1 ^(.).php$ menu.php?result=$1

    But it is giving me error.

    Where an I wrong in the two rules.

    can any one help me.

  21. by Nilesh Vaidya on May 31, 2006 07:45 AM

    First off all a real COOL article Neil, the knowledge provide in it is more than enough to make some good things if anyone is realy interested.

    I have a few question though, if possible could you please guide us on to them:

    Case 1

    www.abc.com/index.php?s=mobiles can easy be converted to www.abc.com/mobiles.htm * It works good fine and preety easy to do that.

    Case 2 ????????

    www.abc.com/index.php?s=mobiles phones www.abc.com/index.php?s=mobiles%20phones

    How can these be converted to www.abc.com/mobiles_phones.htm ????

    Well i am a bit ok with regex, definately not expert nor good but also not dumb, can use them when i want them too. :)

    how will replace from ” ” to “” take over here ? We do not have pregreplace or anything like that….

    Also will using mod_rewrite damage us at the SE side. I mean will google treat them as normal html files.

    Awaiting for your valued reply.

    Thanks, Nilesh

  22. by Anil on July 3, 2006 06:57 AM

    Hi… it’s a great tutorial and has helped me to understand mod_rewrite to some extent. I want to rewrite the following URL but am not bale to figure out how should i do it. If you can give me any pointers it would be helpful.

    <a href="http://localhost/pizzaman/additems.php?mainitemid=1&subitemid=1&itemid=1&sizeid=1">http://localhost/pizzaman/additems.php?mainitemid=1&subitemid=1&itemid=1&sizeid=1</a>
    

    After going through the tutorial i was able to rewrite the URL for first key-value in the query string but how to handle multiple key-values as given above.

  23. by fowmow on July 6, 2006 11:53 AM

    Anil,

    RewriteRule ^pizzaman/([^/.]+)/([^/.]+)/([^/.]+)/([^/.]+)/?$ /pizzaman/additems.php?mainitemid=$1&subitemid=$2&itemid=$3&sizeid=$4 [L]
    

    This allows for a URL like:

    domain.com/pizzaman/one/two/three/four/
    
  24. by Bubs on July 8, 2006 01:41 AM

    Excellent article… thanks so much!

  25. by S Sparkie on July 13, 2006 04:48 PM

    Hi there,

    Really good article that has explained this subject really well but please may I ask if the following is possible? I cannot work it out.

    I have a site tha is written in PHP and pulls data from a MySql DB. The site utilises query strings and I would like to map the query generated pages to the more search engine friendly page titles. For example I would like to acheive to the following mappings:

    content.php?id=1 to map to /aboutus content.php?id=2 to map to /contactus content.php?id=3 to map to /newfiles

    There are about 80 odd pages on the site and would like to know if I would need to map each link as there are all in the format ?id=x or is there a more elegant way to approach this?

    Thanks for any help S

  26. by Isaac on July 17, 2006 05:16 PM

    Thank you for this great tutorial! I am now going to switch to a host with .htaccess enabled so that i can use it!

    I have not found another tutorial this useful (infact any other tutorial that was any good at all!) on the internet!

    Thank you!!!

  27. by Mark on July 21, 2006 08:28 AM

    Wow, this is an excellent tutorial both for the noobs and for the more advanced webdevs.

    Here’s a little tutorial to people who have no idea how to use mod_rewrite:

    If your site consists of only a few pages and you’ve seen the fancy url’s like domain.com/contact rather than domain.com/contact.html you can just use:

    RewriteEngine on RewriteRule ^contact$ /contact.html [L]

    And if you want to hide the filename from being guessed, rename it to con1.html, than you’ll have

    RewriteEngine on RewriteRule ^contact$ /con1.html [L]

    Play around with the forward slashes (/) since one webserver may differ from the other.

  28. by Plamen Markov on July 23, 2006 09:15 PM

    One note for Windows users: When create .htaccess file with Notepad make sure that Word Wrap option is unchecked

  29. by Ken on July 24, 2006 01:30 PM

    Hi,

    Is it possible to use mod_rewrite for src property of img tag

    so assuming that I have this line in html

    then Apache server will rewrite it as

    thanks.

  30. by vex on July 31, 2006 11:24 AM

    Awesome guide, many thanks!

  31. by bobo on July 31, 2006 10:56 PM

    This site helped me a lot, but something is still not ok for me.

    I have to rewrite http://127.0.0.1/apphandler/func1/param1/ to http://127.0.0.1//index.php?function=func1&params=param1

    My rewrite rule is

    RewriteRule ^/apphandler/([^/.]+)/([^/.]+)/?$ /index.php?function=$1&params=$2 [L]
    

    which returns with “No input file specified” instead of the page. If I use [L,R] redirection instead of [L], it is working though. Here is the rewrite.log:

    [rid#7a5450/initial] (2) init rewrite engine with requested uri /apphandler/func1/param1/
    [rid#7a5450/initial] (3) applying pattern '^/apphandler/([^/.]+)/([^/.]+)/?$' to uri '/apphandler/func1/param1/'
    [rid#7a5450/initial] (2) rewrite '/apphandler/func1/param1/' -> '/index.php?function=func1&params=param1'
    [rid#7a5450/initial] (3) split uri=/index.php?function=func1&params=param1 -> uri=/index.php, args=function=func1&params=param1
    [rid#7a5450/initial] (2) local path result: /index.php
    [rid#7a5450/initial] (2) prefixed with document_root to C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/index.php
    [rid#7a5450/initial] (1) go-ahead with C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/index.php [OK]
    [rid#7aab48/initial/redir#1] (2) init rewrite engine with requested uri /php/php-cgi.exe/apphandler/func1/param1/
    [rid#7aab48/initial/redir#1] (3) applying pattern '^/apphandler/([^/.]+)/([^/.]+)/?$' to uri '/php/php-cgi.exe/apphandler/func1/param1/'
    [rid#7aab48/initial/redir#1] (1) pass through /php/php-cgi.exe/apphandler/func1/param1/
    [rid#7ad4d0/subreq] (2) init rewrite engine with requested uri /apphandler/func1/param1/
    [rid#7ad4d0/subreq] (3) applying pattern '^/apphandler/([^/.]+)/([^/.]+)/?$' to uri '/apphandler/func1/param1/'
    [rid#7ad4d0/subreq] (2) rewrite '/apphandler/func1/param1/' -> '/index.php?function=func1&params=param1'
    [rid#7ad4d0/subreq] (3) split uri=/index.php?function=func1&params=param1 -> uri=/index.php, args=function=func1&params=param1
    [rid#7ad4d0/subreq] (2) local path result: /index.php
    [rid#7ad4d0/subreq] (2) prefixed with document_root to C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/index.php
    [rid#7ad4d0/subreq] (1) go-ahead with C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/index.php [OK]
    
  32. by TJ on August 9, 2006 03:10 PM

    Very good article. It really helpful. Thank you so much

  33. by Alex on August 17, 2006 07:11 AM

    Hey, thanks so much for this article.

    I’ve been looking for something like this forever, i’ve just never known how to word it when searching.

    I found you through a google group (php.general).

    Thanks, great article!

  34. by Abhijith on August 27, 2006 09:44 PM

    Hi How Can i make

    www.mysite.com/scripts/main.php?user=username

    work just using the url

    www.mysite.com/username

    Can anyone help>any help will be appreciated.

    Thanks in advance

    Abhijith

  35. by Peter on August 29, 2006 05:43 PM

    Thx, your article gives me a headstart! :-)

  36. by mick on September 10, 2006 05:07 PM

    Hi,

    thanks for the well written article, it “may” have solved my probs for me. As you are well aware there is a huge problem with web application security, in particular flash/php, i have been looking for a solution to be able to invisibly pass something unique to flash from the server thus identifying to all php files that its a legitimate request. With this i think it might stump most hackers as they will never actually manage to find out where they are so i can use the secretly hidden url as a first step security measure and create a dummy index.html with a dummy php code passed to the flash which will never work.

    Do i have any holes in this argument that you can see?

  37. by chris on September 12, 2006 07:46 AM

    Great article - love the simplicity you start off with!

    I want to know if I can use mod_rewrite to test a new host with root relative links, before the domain is resolved to the new host. The temp domain is www.servername.com/~mysite. My images and scripts of course don’t work, so I cannot test. Can I rewrite the document (web) root? I tried the example in Apache’s URL rewriting guide: (Moved Document Root)

    RewriteRule ^/$ /e/www/ [R]

    This has no effect when I try it locally, so maybe I’m missing something. (BTW, your simple example at the top does work). This must have a very simple answer, I’m just new at it! Thank you to anyone who can point me in the right direction!

  38. by Ali on October 25, 2006 07:07 AM

    This will turn /gallerysix into index.php?show=gallerysix

    RewriteEngine On RewriteRule ^([A-Za-z]+)$ index.php?show=$1

    Hope this helps somebody!

  39. by uno on December 16, 2006 03:50 AM

    Case 2 ???????? www.abc.com/index.php?s=mobiles phones www.abc.com/index.php?s=mobiles%20phones

    How can these be converted to www.abc.com/mobiles_phones.htm ????

    Has this been answered? I need to be able to do this also.

  40. by abhimanyu on January 20, 2007 12:17 PM

    U r article helped me a lot about using modrewrite. Thanks for contributing u r knowledge with all. But i am having one question —- Is this true that when we use modrewrite then all the paths to external css files and javascript files are removed from the result page.

    Becoz when i used the code the final page(result page) has no css rules applied to it and the page is getting totally destorted.

    Help me in this problem.Suggest me the solution for the same.

    Thanks a lot, Abhimanyu

  41. by Info web services on January 24, 2007 09:31 AM

    Contents provided here on mod rewrite are great. I too was looking for a way to convert dynamic php pages into static html pages by using mod rewrite command in .ht access file in root.

other relevant pages

about wwm

workingwith.me.uk is a resource for web developers created by Neil Crosby, a web developer who lives and works in London, England. More about the site.

Neil Crosby now blogs at The Code Train and also runs NeilCrosby.com, The Ten Word Review and Everything is Rubbish.